﻿#region File Header
//============================================================================
//  FileName    :	Singleton.cs
//
//  Description :	
//
//  Copyright (C) 2008, GrapeCity Inc.  All rights reserved.
//============================================================================
//
//----------------------------------------------------------------------------
//  Update Log:
//
//  Status                Date                      Name               BUG-ID
//----------------------------------------------------------------------------
// Created         12/16/2008 2:11:34 PM     	    CaiShengRong		 	None
//----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Utility
{
    /// <summary>
    /// 通用类单件模式的选择
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks></remarks>
    /// <history>
    /// [CaiShengRong]   2008/12/16 13:57    Created
    /// </history>
    public class Singleton<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Singleton&lt;T&gt;"/> class.
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [CaiShengRong]   2008/12/16 14:04    Created
        /// </history>
        protected Singleton()
        {
            //Assert class T don't have public constructor
            //Assert class T have a private/protected parameterless constructor
        }

        /// <summary>
        /// 
        /// </summary>
        protected static T _instance;
        /// <summary>
        /// Instances this instance.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// [CaiShengRong]   2008/12/16 13:58    Created
        /// </history>
        public static T Instance
        {
            get
            {
                //Assert class T don't have public constructor
                //Assert class T have a private/protected parameterless constructor
                //Assert class T is not abstract and is not an interface

                if(_instance == null)
                {
                    _instance = (T)Activator.CreateInstance(typeof(T), true);//use reflection to create instance of T
                }
                return _instance;
            }
           
        }

        /// <summary>
        /// Destroys this instance.
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [CaiShengRong]   2008/12/16 13:58    Created
        /// </history>
        public void Destroy()
        {
            _instance = default(T);
        }
    }

   
}
